package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.util.R;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PostMapping("/list")
    public R list(Integer page, Integer rows, String  customerName){
        Page<Customer> pages = customerService.page(page,rows,customerName);
        return R.ok().put("rows",pages);
    }

    @PostMapping("/save")
    public ServiceVO saveOrUpate(Customer customer, Integer customerId){
        if (customerId == null){
            customerService.save(customer);
            return new ServiceVO(100,"添加成功");
        }else {
            customerService.update(customer,customerId);
            return new ServiceVO(100,"修改成功");
        }

    }

    @PostMapping("/delete")
    public ServiceVO delete(String  ids){
        customerService.delete(ids);
        return new ServiceVO(100,"删除成功");
    }
}
